package com.zl.tcbnewcar.ui.activity;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.TextView;

import com.zl.tcbnewcar.R;
import com.zl.tcbnewcar.base.BaseActivity;
import com.zl.tcbnewcar.utils.API;
import com.zl.tcbnewcar.utils.HttpUtils;
import com.zl.tcbnewcar.utils.SpUtlis;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;


/**
 * @author zhanglei
 * @date 17/8/6
 * 启动页
 */
public class LauncherActivity extends BaseActivity {

    @BindView(R.id.tv_version_name)
    TextView tvVersionName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //取消状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_launcher);
        ButterKnife.bind(this);
        //获取当前APP的版本号
        try {
            final PackageInfo packageInfo = mActivity.getPackageManager().getPackageInfo(mActivity.getPackageName(), 0);
            tvVersionName.setText("V" + packageInfo.versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        SpUtlis.setLocationData(mActivity, "", "全国城市", "全国城市");

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                toActivity();
            }
        }, 1500);
    }

    /**
     * 进入引导和广告页的逻辑处理
     */
    private void toActivity() {
        PackageInfo pkg = null;
        try {
            pkg = getPackageManager().getPackageInfo(getApplication().getPackageName(), 0);
            int versionCode = SpUtlis.getVersionCode(LauncherActivity.this);
            if (versionCode == -1) {
                //储存当前版本号
                SpUtlis.setVersionCode(LauncherActivity.this, pkg.versionCode);
                //进入欢迎页
                startActivity(new Intent(LauncherActivity.this, WelComeActivity.class));
            } else {
                if (versionCode != pkg.versionCode) {
                    //储存当前版本号
                    SpUtlis.setVersionCode(LauncherActivity.this, pkg.versionCode);
                    //进入欢迎页
                    startActivity(new Intent(LauncherActivity.this, WelComeActivity.class));
                } else {
                    HttpUtils.getInstance().GET(mActivity, API.getWelcomAdvert, new HttpUtils.OnOkHttpCallback() {
                        @Override
                        public void onSuccess(String body) {
                            try {
                                JSONObject object = new JSONObject(body);
                                if (!object.optBoolean("result")) {
                                    //进入首页
                                    startActivity(new Intent(LauncherActivity.this, MainActivity.class));
                                } else {
                                    //进入广告页
                                    startActivity(new Intent(LauncherActivity.this, ADActivity.class));
                                }
                                finish();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }

                        @Override
                        public void onError(Request error, Exception e) {

                        }
                    });

                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
}
